package utils;

import java.io.*;
import java.util.Properties;
import org.apache.commons.logging.Log;

/**
 *
 * @author gurgel
 */
public class SmProperties implements Serializable {

    private static Properties props;
    private static SmProperties instance;
    private static String propArqName = "properties.config";
    private static final Log log = SmLogFactory.getLog(SmProperties.class);
    private static final String comments = "config file:\n"
            + "dbDatabaseType=postgresql|mysql"+"\n"
            + "dbDriver=org.postgresql.Driver|com.mysql.jdbc.Driver"+"\n"
            + "dbUser=userName"+"\n"
            + "dbPassword=password"+"\n"
            + "dbDatabase=database"+"\n"
            + "dpIpAddress=localhost|10.1.7.19"+"\n"
            + "dbPortAddress=5432|3306"+"\n"
            + "\n";

    /**
     * Creates a new instance of SmProperties
     */
    private SmProperties() {
        props = new Properties();
        if (!this.load()) {
            this.newFile(true);
        }
    }

    public synchronized static SmProperties getInstance() {
        if (SmProperties.instance == null) {
            SmProperties.instance = new SmProperties();
        }
        return SmProperties.instance;
    }

    private static Properties getProperties() {
        if (props == null) {
            SmProperties smProperties = new SmProperties();
        }
        return props;
    }

    public static String getProperty(String key) {
        return getProperties().getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        return getProperties().getProperty(key, defaultValue);
    }

    /**
     * read the properties file.
     *
     */
    private boolean load() {
        try {
            InputStream propIn = new FileInputStream(new File(getPropArq()));
            getProperties().load(propIn);
            return true;
        } catch (IOException e) {
            //file not found
            return false;
        }
    }

    /**
     * create a new file
     */
    private boolean newFile(boolean newFile) {
        try {
            log.info("Creating new file: " + getPropArq());
            OutputStream propFile = new FileOutputStream(new File(getPropArq()));
            if (newFile) {
                this.setPropDefault();
            }
            getProperties().store(propFile, comments);
            return true;
        } catch (IOException e) {
            log.error("Error while creating file.");
            return false;
        }
    }

    private String getPropArq() {
        return propArqName;
    }

    public void setProp(String key, String value) {
        log.debug("Saving config file.");
        getProperties().setProperty(key, value);
        try {
            OutputStream propFile = new FileOutputStream(new File(getPropArq()));
            getProperties().store(propFile, comments);
        } catch (IOException ex) {
            log.fatal(ex);
            throw new UnsupportedOperationException("Full Stop!");
        }
    }

    private void setPropDefault() {
        getProperties().setProperty("dbDatabaseType", "postgresql");
        getProperties().setProperty("dbDriver", "org.postgresql.Driver");
        getProperties().setProperty("dbUser", "javaUser");
        getProperties().setProperty("dbPassword", "Project123!");
        getProperties().setProperty("dbDatabase", "SmartProjects");
        getProperties().setProperty("dpIpAddress", "10.1.7.19");
        getProperties().setProperty("dbPortAddress", "5432");
    }
}
